import * as THREE from '../../libs/threejs/three.min'
import {
  OrbitControls
} from '../../libs/threejs/OrbitControls'
import GLTFadapter from '../../libs/threejs/GLTFLoader'
import {
  assetDatas
} from '../../utils/util'

let scene = new THREE.Scene();
var camera, renderer, controls, hemiLight, dirLight, gltfLoader, modelUrl, model, clock, mixer, anima1,render


const initScene = (canvas, type = 0) => {
  renderer = new THREE.WebGLRenderer({
    alpha: true,
  });
  let cameraAspect = canvas.width / canvas.height
  camera = new THREE.PerspectiveCamera(45, cameraAspect, 0.25, 100);
  camera.position.set(5, 3, 10);
  camera.lookAt(new THREE.Vector3(0, 2, 0));
  const devicePixelRatio = wx.getSystemInfoSync().pixelRatio;
  renderer.setPixelRatio(devicePixelRatio < 3 ? devicePixelRatio : 2);
  renderer.setSize(canvas.width, canvas.height);

  controls = new OrbitControls(camera, renderer.domElement);
  controls.maxDistance = 15
  controls.minDistance = 5
  controls.update();

  // init lights
  // let hemiLight = null;
  if (type == 1) {
    hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 6);
  } else {
    hemiLight = new THREE.HemisphereLight(0xffffff, 0xffffff, 1);
  }
  hemiLight.position.set(0, 0, 0);
  scene.add(hemiLight);

  dirLight = new THREE.DirectionalLight(0xffffff, 1);
  dirLight.position.set(0, 10, 10);
  scene.add(dirLight);


  // var ambient = new THREE.AmbientLight(0xffffff, 2) //
  // ambient.position.set(10, 10, 10)
  // scene.add(ambient)


  // init Sound
  const app = getApp()
  const avatarSound = app.globalData.innerAudioContext
  console.log(assetDatas[type].sound, "路径对象")
  avatarSound.src = assetDatas[type].sound

  // init Title
  // wx.setNavigationBarTitle({
  //   title: assetDatas[type].title
  // })

  // add Avatar
  const GLTFLoader = GLTFadapter(THREE)
  const gltfLoader = new GLTFLoader()
  const modelUrl = assetDatas[type].model
  // let mixer, anima1
  gltfLoader.load(modelUrl, (gltf) => {
    const model = gltf.scene
    scene.add(model)
    model.position.set(0, -2.5, 1)
    model.scale.set(2, 2, 2);
    // model.rotation.x=Math./12;
    // model.rotation.x=Math/12;
    model.rotation.y = Math.PI / 6;
    dirLight.target = model
    mixer = new THREE.AnimationMixer(model);
    anima1 = mixer.clipAction(gltf.animations[0])
    anima1.setLoop(THREE.LoopOnce, 10000)
    anima1.play()
    avatarSound.play()
  })

  clock = new THREE.Clock();
  render = () => {
    canvas.requestAnimationFrame(render)
    if (mixer != null) {
      mixer.update(clock.getDelta());
    };
    controls.update();
    renderer.render(scene, camera);
  }
  render()
}



const remove = function () { //remove只是移出父子关系，还要将其设置为null
  // flog = 1;
  mixer = null
  scene.remove(hemiLight); //移出灯光
  hemiLight = null;
  scene.remove(dirLight); //移出灯光
  dirLight = null;
  // scene.remove(light); //移出灯光
  // var camera,renderer,controls,hemiLight,dirLight,gltfLoader,modelUrl,model,clock

  console.log("移出模型前先判断model:", model)
  if (model != undefined) {
    scene.remove(model); //移出模型
    model = null;
    console.log("已有模型就移出模型")
  }
  
  // scene.remove(camera); //移出相机
  // camera = null;
  scene.dispose(); //注销场景
  scene = null;
  // if(renderer){
  //   renderer.dispose();
  //   renderer = null;
  // }
  //画布每次创建id不一样，可以退出时，销毁画布
}

const hideModel = function () {
  scene.visible = false
}

const showModel = function () {
  scene.visible = true
}

module.exports = {
  initScene,
  hideModel,
  showModel,
  remove
}